﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;
using RestSharp;
using Openapi.Weibo;
using Openapi.Weibo.Model;
using System.IO;

namespace Sample.Consoles
{
    class Program
    {
        static void Main(string[] args)
        {
            //var presons = JsonHelper.JsonToObject<Presons>("{\"count\":2,\"presons\":[{\"name\":\"zhangsan\",\"age\":20,\"sex\":true},{\"name\":\"lisi\",\"age\":22,\"sex\":false}]}");
            string str = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "Statuses.json");
            var statuses = JsonHelper.JsonToObject<Statuses>(str);
            ConsoleKeyInfo info;
            Console.Write("请输入用户名：");
            string userName = Console.ReadLine();
            Console.Write("请输入密码：");

            string password = String.Empty;
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter && info.Key != ConsoleKey.Backspace && info.Key != ConsoleKey.Escape && info.Key != ConsoleKey.Tab && info.KeyChar != '\0')
                {
                    password += info.KeyChar;
                    Console.Write('*');
                }
            } while (info.Key != ConsoleKey.Enter);
            //"2928666766", "dd2eb9562b2243c084140320e5b066ca"
            OAuthAuthorization OAuth = new OAuthAuthorization();
            var accessToken=new AccessToken();
            bool flag =OAuth.SimulateLogin(userName, password, out accessToken);
            if (flag)
            {
                Client client = new Client(accessToken);
                var f= client.VerifyCredentials(accessToken.access_token);
                MemoryCache.Default.Add(accessToken.uid,accessToken,DateTime.Now.AddHours(2));
                //var data = client.ExecuteApi("statuses/friends_timeline.json",Method.GET);
                var data = client.Statuses.PublicTimeline(200);
                Console.WriteLine(JsonHelper.ObjectToJson<Statuses>(data));
            }
            Console.Read();
        }
    }
    public class Preson
    {
        public string name { get; set; }
        public int age { get; set; }
        public bool sex { get; set; }
    }

    public class Presons
    {
        public Preson[] presons { get; set; }
        public int count { get; set; }
    }
}
